<?php
namespace Orm\Coupon;

use Base\Exception\RmException;
class CouponClass extends \Base\BaseOrmObject{
    protected   $id,
				$code = '',
				$is_use = 0, // 0未使用 1已使用
				$is_del = 0,
				$status = 0,
				$insert_time = '',
				$update_time = '';

    const IS_USE_ZERO = 0; // 0未使用
    const IS_USE_ONE = 1; // 1已使用

    const STATUS_YES = 1;// 已在线下使用
    const STATUS_NO = 0;//为使用



    public static function getIsUseList($key = '')
    {
        $list = array(
            self::IS_USE_ZERO => '未中奖',
            self::IS_USE_ONE => '已中奖',
        );
        if ($key != '') {
            if (isset($list[$key])) {
                return $list[$key];
            } else {
                return '未知';
            }
        }
        return $list;
    }
    public static function getIsStatusList($key = '')
    {
        $list = array(
            self::STATUS_YES => '已使用',
            self::STATUS_NO => '未使用',
        );
        if ($key != '') {
            if (isset($list[$key])) {
                return $list[$key];
            } else {
                return '未知';
            }
        }
        return $list;
    }

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\Coupon\CouponModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'code' => $this->code,
			'is_use' => $this->is_use,
			'status' => $this->status,
        );

        $model = new \Orm\Coupon\CouponModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'code' => $this->code,
			'is_use' => $this->is_use,
			'status' => $this->status,
        );

        $model = new \Orm\Coupon\CouponModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\Coupon\CouponModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }
}